VERSION 1.0 CLASS
BEGIN
  MultiUse = -1  'True
  Persistable = 0  'NotPersistable
  DataBindingBehavior = 0  'vbNone
  DataSourceBehavior  = 0  'vbNone
  MTSTransactionMode  = 0  'NotAnMTSObject
END
Attribute VB_Name = "Settings"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = True
Attribute VB_PredeclaredId = False
Attribute VB_Exposed = False
Option Explicit

'TODO: Insert standard header

' The maximum number of shapes displayed that can be displayed at any one time.
Private m_bMaximumShapesDisplayed As Byte

' The absolute path to the file where settings are persisted.
' E.g. C:\Documents and Settings\ChrisW\Application Data\Chris Watson\Circles
'      \Settings.dat
Dim SettingsFilePath As String

'#Region Public Properties

' Summary:
'   Get the maximum number of shapes displayed that can be displayed at any one
'   time.
Public Property Get MaximumShapesDisplayed() As Byte
    MaximumShapesDisplayed = m_bMaximumShapesDisplayed
End Property

Public Property Let MaximumShapesDisplayed(ByVal bNewValue As Byte)
    m_bMaximumShapesDisplayed = bNewValue
End Property

'#End Region

'#Region Private Methods

' Summary:
'   Builds the absolute path to location of the file where the settings will be
'   persisted.
Private Function GetSettingsFilePath() As String
    Dim Path As String
    
    ' Start with the location of the application data folder
    ' TODO: There's a WIN32 API call that can obtain special folder locations,
    '       I'd prefer to use that.
    Path = Environ("APPDATA") & "\"
    
    ' Append the name of the Company as a folder
    Path = Path & App.CompanyName & "\"
    
    ' Append the name of the application as a folder
    Path = Path & App.Title & "\"
    
    ' Append the name of the file
    Path = Path & "Settings.dat"
    
    Debug.Print Path
    
    GetSettingsFilePath = Path
End Function

' Summary:
'   Reads all settings from the settings file.
Private Sub ReadSettingsFile()
    Me.MaximumShapesDisplayed = _
        SettingsHelper.ParseMaximumShapesDisplayed( _
            ReadValueFromSettingsFile( _
                SettingsHelper.MaximumShapesDisplayed_SettingName, _
                SettingsHelper.MaximumShapesDisplayed_DefaultValue))
End Sub

' Summary:
'   Obtains the value of the setting with the specified name, or if it is not
'   set, the default value.
' Params:
'   ItemName:
'     The name of the setting to obtain.
'   DefaultValue:
'     The default value to be returned if no value is set in the settings file.
' Returns:
'   String. The value of the specified setting in the settings file, or the
'   default value if no value is set in the settings file.
Private Function ReadValueFromSettingsFile(ByVal Name As String, _
  ByRef DefaultValue As String) As String
    
    Dim Value As String
    Dim ValueIsSet As Boolean ' Item set in settings file?
    
    Dim FileNumber
    FileNumber = FreeFile
    
    Dim TextLine ' Holds each line of the settings file as we iterate
    
    Open SettingsFilePath For Input As #FileNumber
    
    Do While Not EOF(FileNumber)
        Line Input #FileNumber, TextLine
        Debug.Print TextLine
        
        ' Each line should be in the form:
        ' Name:Value
        
        Const Seperator As String * 1 = ":"
        
        Dim LineItemName As String
        Dim LineItemValue As String
        
        Dim PosSeperator As Integer
        PosSeperator = InStr(1, TextLine, Seperator)
        
        If PosSeperator Then
            LineItemName = Trim(Left(TextLine, PosSeperator - 1))
            LineItemValue = Mid(TextLine, PosSeperator + 1)
        Else
            LineItemName = ""
            LineItemValue = ""
        End If
        
        Debug.Print "Name: """; LineItemName; """, Value: """; LineItemValue; """"
        
        If LineItemName = Name Then
            Value = LineItemValue
            ValueIsSet = True
            Exit Do
        End If
    Loop
    
    Close #FileNumber
    
    If Not ValueIsSet Then
        Value = DefaultValue
    End If
    
    ReadValueFromSettingsFile = Value
End Function

'#End Region

'#Region Event Handlers

' Summary:
'   Event handler for the Class' Initialize event.
Private Sub Class_Initialize()
    ' Build the path to the settings file in app data folder
    SettingsFilePath = GetSettingsFilePath()
    
    ' Read in all settings from the settings file
    ReadSettingsFile
End Sub

'#End Region
